//
//  gl.h
//  gltest
//
//  Created by jiajiawei on 12-12-28.
//  Copyright (c) 2012年 jiajiawei. All rights reserved.
//

#ifndef gltest_gl_h
#define gltest_gl_h
#import <vector>

#define GL_PI 3.1415926f

struct GLPoint
{
    float x;
    float y;
    float z;
    float w;
    float s;
    float t;
};

typedef struct GLPoint GLPoint;

void textureInit();

unsigned int* image_data();
unsigned int image_data_size();
float* modeviewM();
float* frustumM();


void drawLine(float x1, float y1, float x2, float y2, unsigned int color1, unsigned int color2);
void drawTriagle(float x1, float y1, float x2, float y2, float x3, float y3, unsigned int color1, unsigned int color2, unsigned int color3);
void drawSTriagle(std::vector<GLPoint>& vpoints);
void drawTSTriagle(std::vector<GLPoint>& vpoints);





struct GLRect
{
    float left;
    float right;
    float top;
    float bottom;
};

typedef struct GLRect GLRect;




GLPoint glPointMake(float x, float y, float z, float w);
void glPointSet(GLPoint* point, float x, float y, float z, float w);
void glPointNormal(GLPoint* point);

GLRect GLRectMake(float left, float top, float right, float bottom);

int clipSegment(GLPoint* point1, GLPoint* point2, GLRect* window);


int computeBC(GLPoint* point, float pointBC[6]);


int clipEdge(GLPoint* point1, GLPoint* point2);

void insertPoint(GLPoint* points, int* count, GLPoint point, int index);
void removePoint(GLPoint* points, int* count, int index);
int clipTriagle(GLPoint* points, int* count);

struct GLVector
{

    float x;
    float y;
    float z;
    float n;
};

typedef struct GLVector GLVector;

GLVector glVectorMake(float x, float y, float z);
void glVectorSet(GLVector* v, float x, float y, float z);
void glVectorSub(GLVector* v1, GLVector* v2, GLVector* v);
void glNormalVector(GLVector* v);
float glVectorMul(GLVector* v1, GLVector* v2);
void glVectorCross(GLVector* v1, GLVector* v2, GLVector* v);

void glLoadIdentifier();
void glLoadMatrix(float m[16]);
void glMulMatrix(float m[16]);
void glMatrixMul(float m1[16], float m2[16], float m[16]);
void glTranslate(float x, float y, float z);
void glScale(float x, float y, float z);
void glRotate(float angle, float x, float y, float z);

void glTransformPoint(float matrix[16], GLPoint* point);

struct GLCamera
{
    GLPoint eye, look;
    GLVector up, u, v, n;
    float CT[16];
    float viwAngle, aspect, newrDist, farDist;
};

typedef struct GLCamera GLCamera;

void glCameraSet(GLCamera* camera, float eyex, float eyey, float eyez, float lookx, float looky, float lookz, float upx, float upy, float upz);
void glCameraSlide(GLCamera* camera, float du, float dv, float dn);
void glCameraRoll(GLCamera* camera, float angle);
void glCameraPitch(GLCamera* camera, float angle);
void glCameraYaw(GLCamera* camera, float angle);
void glLoadMatrixFromCamera(GLCamera* camera);

void gluLookAt(float eyex, float eyey, float eyez, float lookx, float looky, float lookz, float upx, float upy, float upz);

void glFrustum(float left, float right, float bottom, float top, float near, float far);

void glClear();

void newtonC(float points[], float c[], int nd, int count);
void newtonInterpolation(float points[], int nd, int count, float pvs[], int pvcount);

#endif
